<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\StoreModel;
use app\admin\model\AdminMenuModel;

class StoreController extends AdminBaseController
{

    public function initialize()
    {
        $this->cate_model = Db::name("Cate");
        $this->deal_model = Db::name("Goods");
		$this->room_model = Db::name("room");
		$this->log_model = Db::name("outinLog");
		//$this->room_model = new StoreModel();
        parent::initialize();
    }
	/*库房管理*/
	public function room(){
        $data = $this->request->param('room_name','');
        //取库房数据
        $results = $this->room_model->alias('d')
                ->field('d.*,u.user_login')
                ->join('dj_user u','u.id = d.add_user','left')
                ->where('room_name','like','%'.$data.'%')
                ->where('d.is_delete',0)
                ->order('d.id', 'DESC')->paginate(12);
		$this->assign("results",$results);
        // 页面传值
        $page = $results->render();
        $this->assign("page", $page);
		return $this->fetch();
	}
	/*库房管理添加跳转*/
	public function add(){
		return $this->fetch();
	}
	/*库房管理添加*/
	public function add_post(){
		$data = $this->request->param();
		$result = $this->validate($data, 'Store');
		if ($result !== true) {
            $this->error($result);
        }

		$data['add_time'] = time();
		$data['add_user'] = cmf_get_current_admin_id();
		//$this->room_model->allowField(true)->data($data, true)->isUpdate(false)->save();
		$this->room_model->insertGetId($data);
		$this->success("添加成功", url('Store/room'));
	}
    /**
     * 物理删除数据
     **/
    public function delete(){
        // 需要删除的数据ID
        $id = $this->request->param("id",0,'intval');
        // 判断是否为物理删除
        //dump($this->config);exit;
        if($this->config['REAL_DELETE']){
            if ($this->room_model->where(array('id'=>$id))->delete()!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        } else {
            $saveData = array(
                'id'		=> $id,
                'is_delete'	=> 1
            );
            if ($this->room_model->update($saveData)!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }
    /**
     * 切换数据显隐状态
     **/
    public function toggle(){
        $id = $this->request->param("id", 0, 'intval');
        if(!$id){
            $this->error("未选择数据！");
        }

        $status = $this->request->param("show", '0');
        $field = $this->request->param("field", 'if_show');

        $saveData = array(
            'id'	=> $id,
            $field	=> $status == '0' ? '1' : '0'
        );
        if ($this->room_model->update($saveData) !== false) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }
    /*库房管理编辑跳转*/
    public function edit(){
        $id = $this->request->param("id",0,'intval');
        $where = array(
            'id'	=> $id
        );
        $result = $this->room_model->where($where)->find();
        $this->assign($result);
        return $this->fetch();
    }
    /**
     * 修改保存
     **/
    public function edit_post(){
        if ($this->request->isPost()) {
            $arrData  = $this->request->post();
            $this->room_model->update($arrData);
            $this->success(lang("EDIT_SUCCESS"), url("Store/room"));
        }
    }
	/*进出库管理*/
	public function index(){
        $rooms=$this->room_model->where(['if_show'=>1,'is_delete'=>0])->select();
        $this->assign('rooms',$rooms);


        $type = $this->request->param("type",0);
        $this->assign("type",empty($type) ? '' : $type);

        $where = ['d.type'=>$type,'d.is_delete'=>0,'d.if_show'=>1];
        //商品名称
        $data = $this->request->param('goods_name','');
        //库房名
        $room = $this->request->param("room",0);
        $this->assign('room',$room);
        empty($room)?$where:($where=['d.type'=>$type,'d.is_delete'=>0,'d.if_show'=>1,'r.id'=>$room]);
        //取库房数据
        $results = $this->deal_model
            ->alias("d")
            ->field("d.*,'物料' as cate_name,r.room_name")
            ->join("dj_room r","r.id = d.room","left")
            ->where('d.goods_name','like','%'.$data.'%')->where($where)->order('d.id', 'DESC')->paginate(12);
        $this->assign("results",$results);
        // 页面传值
        $page = $results->render();
        $this->assign("page", $page);
        return $this->fetch();
	}
	/*
	 * 出库
	 */
	public function  outgoods(){
	    $data = $this->request->param();
	    $this->assign($data);
	    return $this->fetch();
    }
    /*
     * 出货、补货
     */
    /**
     * @return array
     */
    public function goods_post()
    {
        $data = $this->request->param();
        if(empty($data['num'])){
            $this->error("请填写数量");
        }
        if(!preg_match("/^[1-9][0-9]*$/",$data['num'])){
            $this->error('数量只能为正整数');
        }

        //dump($data);exit;
        if($data['from']=='add'){
            $this->deal_model->where("id",$data['goods_id'])->setInc('stock',$data['num']);
        }elseif ($data['from']=='out'){
            // 判断出货数量是否超过库存数量
            $g = $this->deal_model->where("id",$data['goods_id'])->column('stock');
            if($g[0] < $data['num']){
                $this->error("出库数量超过库存数量！");
            }else{
                $this->deal_model->where("id",$data['goods_id'])->setDec('stock',$data['num']);
            }
        }

        $data['add_user'] = cmf_get_current_admin_id();
        $data['add_time'] = time();
        $this->log_model->insertGetId($data);
        $this->success("添加成功");
    }
	/*记录*/
	public function log(){
        $rooms=$this->room_model->where(['if_show'=>1,'is_delete'=>0])->select();
        $this->assign('rooms',$rooms);
        $type = $this->request->param("type",0);
        $this->assign("type",empty($type) ? '' : $type);
        $from = $this->request->param("from",'');
        $where = ['g.type'=>$type,'d.type'=>$type,'d.is_delete'=>0];
        $from=='all'||empty($from)?$where:($where = ['d.type'=>$type,'d.is_delete'=>0,'d.from'=>$from]);
        $this->assign("from",$from);

        $room = $this->request->param("room",0);
        $this->assign('room',$room);
        empty($room)?$where:($where['r.id']=$room);
        //商品名称
        $data = $this->request->param('goods_name','');
        //取库房数据
        $results = $this->log_model
            ->alias("d")
            ->field("d.*,c.cate_name,g.goods_name,r.room_name,u.user_login")
            ->join("dj_goods g","g.id = d.goods_id","left")
            ->join("dj_cate c","g.cate_p_id = c.id","left")
            ->join("dj_room r","r.id = g.room","left")
            ->join("dj_user u","u.id = d.add_user","left")
            ->where('g.goods_name','like','%'.$data.'%')->where($where)->order('d.id', 'DESC')->paginate(12);
        $this->assign("results",$results);

        // 页面传值
        $page = $results->render();
        $this->assign("page", $page);
		return $this->fetch("log");
	}
	/*
	 * 删除出入库操作
	 */
	public  function  delete_log()
    {
        // 需要删除的数据ID
        $id = $this->request->param("id",0,'intval');
        $saveData = array(
            'id'		=> $id,
            'is_delete'	=> 1
        );
        if ($this->log_model->update($saveData)!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
}